package cn.tyoui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.YfzlBean;
import cn.tyoui.service.YfzlManageService;

@Controller
public class YfzlController {

	@Resource
	private YfzlManageService yfzlManageService;
	
	@ResponseBody
	@RequestMapping("btn_delete_yfzl")
	public Map<String,Object> deleteYfzl(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		String id = map.get("id");
		System.out.println("id : " + id);
		boolean isSuccess = false;
		if(id != null && !id.equals("")) {
			try {
				yfzlManageService.deleteYfzl(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			isSuccess = true;
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("change_yfzl")
	public Map<String,Object> changeyfzl(@RequestBody YfzlBean yfzl){
		System.out.println("change_yfzl");
		System.out.println(yfzl);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(yfzl != null && yfzl.getId() != 0 && yfzl.getYfzl()!=null && !yfzl.getYfzl().equals("")) {
			try {
				List<YfzlBean> yfzls = yfzlManageService.queryYfzl(yfzl);
				if(yfzls.size() == 0) {
					yfzlManageService.changeYfzl(yfzl);
					isSuccess = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("query_yfzl")
	public Map<String,Object> queryYfzl(@RequestBody YfzlBean yfzl){
		System.out.println("query_yfzl");
		System.out.println(yfzl);
		Map<String,Object> resultMap = new HashMap<>();
		if(yfzl != null) {
			try {
				List<YfzlBean> yfzls = yfzlManageService.queryYfzl(yfzl);
				System.out.println(yfzls.size());
				resultMap.put("yfzls", yfzls);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("add_yfzl")
	public Map<String,Object> addYfzl(@RequestBody YfzlBean yfzl){
		System.out.println("add_yfzl");
		System.out.println(yfzl);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(yfzl != null && !yfzl.getYfzl().equals("")) {
			try {
				List<YfzlBean> yfzls = yfzlManageService.queryYfzl(yfzl);
				System.out.println(yfzls.size());
				if(yfzls.size() == 0) {
					isSuccess = yfzlManageService.addYfzl(yfzl);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
}
